﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using REVISIONMAX30.DataObjects;

namespace REVISIONMAX30
{
    public partial class frmConnectionString : Form
    {
        public frmConnectionString()
        {
            InitializeComponent();
        }

        public UIHandler uiHandler { get; set; }
        public DataObjects.ConnectionString connectionString { get; set; }
        public DataObjects.Group group { get; set; }

        public void View(UIHandler pUIHandler, DataObjects.ConnectionString pChildObject, DataObjects.Group pParentObject)
        {
            this.uiHandler = pUIHandler;
            this.connectionString = pChildObject;
            this.group = pParentObject;
            this.UpdateViewer();
            base.ShowDialog();
        }


        public Guid DataObjectGUID
        {
            get { return this.connectionString.DataObjectGUID; }
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            this.Text = "Connection String: " + this.txtName.Text;
        }

        private void UpdateObject()
        {
            this.connectionString.Name = this.txtName.Text;
            this.connectionString.SetValue(
                this.txtServer.Text,
                this.cboDataBase.Text,
                this.txtPort.Text.UIntValueOrDefault(0xe8c),
                this.txtUserID.Text,
                this.txtPassword.Text);
        }

        private void UpdateViewer()
        {
            this.txtName.Text = this.connectionString.Name;

            this.cboDataBase.Text = this.connectionString.ConnectionStringBuilder.Database;
            this.txtServer.Text = this.connectionString.ConnectionStringBuilder.Server;
            this.txtPort.Text = this.connectionString.ConnectionStringBuilder.Port.StringValueOrEmpty();
            this.txtUserID.Text = this.connectionString.ConnectionStringBuilder.UserID;
            this.txtPassword.Text = this.connectionString.ConnectionStringBuilder.Password;
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            this.Save(true);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.Save(false);
        }

        public void Save(bool pApply)
        {
            UpdateObject();

            if (!group.ConnectionStringList.Exists(t => t.DataObjectGUID == this.DataObjectGUID))
            {
                group.ConnectionStringList.Add(this.connectionString);
            }

            if (!pApply)
            {
                this.Close();
            }

        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            this.UpdateObject();
            var tFrResult = this.connectionString.TestConnection();

            MessageBox.Show(tFrResult.Message);
        }

        private MySql.Data.MySqlClient.MySqlConnectionStringBuilder connectionStringBuilder = null;

        private void cboDataBase_DropDown(object sender, EventArgs e)
        {

            if (this.connectionStringBuilder == null)
            {
                this.connectionStringBuilder = new MySql.Data.MySqlClient.MySqlConnectionStringBuilder();
            }

            if (this.connectionStringBuilder.Server != this.txtServer.Text ||
            this.connectionStringBuilder.Port != this.txtPort.Text.UIntValueOrDefault(0xe8c) ||
            this.connectionStringBuilder.UserID != this.txtUserID.Text ||
            this.connectionStringBuilder.Password != this.txtPassword.Text)
            {
                this.connectionStringBuilder.Server = this.txtServer.Text;
                this.connectionStringBuilder.Port = this.txtPort.Text.UIntValueOrDefault(0xe8c);
                this.connectionStringBuilder.UserID = this.txtUserID.Text;
                this.connectionStringBuilder.Password = this.txtPassword.Text;

                if (MySQLDBHandler.TestConnection(this.connectionStringBuilder.ConnectionString).Severity != Frame.FrResultSeverityType.Error)
                {
                    MySQLDBHandler.FillListWithDatabaseNames(cboDataBase.Items, this.connectionStringBuilder.ConnectionString);
                }
                else
                {
                    cboDataBase.Items.Clear();
                }

            }
        }
    }
}
